package com.pcbly.web.components.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

@SuppressWarnings("unchecked")
public class TypeTransformOp {
	private static final Logger log = LogManager
			.getLogger(TypeTransformOp.class);

	
	public static Class loadClass(String className) throws Exception {
		if (className == null || className.equalsIgnoreCase("null")) {
			return null;
		} else {
			Class result = Class.forName(className);
			return result;
		}
	}

	public static Class[] loadClasses(String classNames[]) throws Exception {
		if (classNames == null || classNames.length == 0
				|| classNames[0].equalsIgnoreCase("null"))
			return null;
		Class result[] = new Class[classNames.length];
		for (int i = 0; i < classNames.length; i++)
			result[i] = loadClass(classNames[i]);

		return result;
	}

	public static Object loadObject(String className, String objectStrValue)
			throws Exception {
		if (className == null || className.equalsIgnoreCase("null"))
			return null;
		Object result;
		if (className.equalsIgnoreCase("java.util.Date"))
			result = TimeOp.convertStrToDateTime(objectStrValue);
		else
			result = ConvertUtils.convert(objectStrValue, Class
					.forName(className));
		return result;
	}

	public static Object[] loadObjects(String classNames[],
			String objectStrValues[]) throws Exception {
		if (classNames == null || classNames.length == 0
				|| classNames[0].equalsIgnoreCase("null"))
			return null;
		Object result[] = new Object[classNames.length];
		for (int i = 0; i < classNames.length; i++)
			result[i] = loadObject(classNames[i], objectStrValues[i]);

		return result;
	}

	public static Object[] loadObjects(Class clazzes[],
			String objectStrValues[]) throws Exception {
		if (clazzes == null || clazzes[0] == null)
			return null;
		Object result[] = new Object[clazzes.length];
		for (int i = 0; i < clazzes.length; i++)
			result[i] = loadObject(clazzes[i].getName(), objectStrValues[i]);

		return result;
	}

	public static synchronized String getDecimalFormatNumber(double number,
			String pattern) {
		DecimalFormat df = (DecimalFormat) NumberFormat
				.getInstance(Locale.CHINA);
		df.setGroupingSize(4);
		df.applyPattern(pattern);
		return df.format(number);
	}

	public static ArrayList StringArrayToArrayList(String strArr[]) {
		ArrayList result = new ArrayList();
		for (int i = 0; i < strArr.length; i++)
			result.add(strArr[i]);

		return result;
	}

	public static Properties mapToProperties(AbstractMap map) {
		Properties result = null;
		Set keySet = map.keySet();
		if (keySet.size() > 0) {
			result = new Properties();
			String keyName;
			for (Iterator keyIter = keySet.iterator(); keyIter.hasNext(); result
					.put(keyName, map.get(keyName)))
				keyName = (String) keyIter.next();

			return result;
		} else {
			return null;
		}
	}

	public static String ObjectToString(Object obj) {
		return ConvertUtils.convert(obj);
	}

	public static boolean compareClassArray(Class class01[], Class class02[]) {
		if (class01 == null && class02 == null) {
			log.debug("Class\u6570\u7EC4\u5747\u4E3Anull");
			return true;
		}
		if (class01 != null && class02 != null) {
			if (class01.length != class02.length) {
				log
						.debug("Class\u6570\u7EC4\u6570\u91CF\u4E0D\u4E00\u81F4:Class\u6570\u7EC401\u4E3A"
								+ class01.length
								+ ",Class\u6570\u7EC402\u4E3A"
								+ class02.length);
				return false;
			}
			List compareList01 = new ArrayList();
			List compareList02 = new ArrayList();
			for (int i = 0; i < class01.length; i++) {
				compareList01.add(class01[i]);
				compareList02.add(class02[i]);
			}

			for (int i = 0; i < compareList01.size(); i++)
				if (compareList02.contains(compareList01.get(i))) {
					compareList02.remove(compareList01.get(i));
					compareList01.remove(compareList01.get(i));
				}

			if (compareList02.size() == compareList01.size()
					&& compareList01.size() == 0)
				return true;
			StringBuffer sb = new StringBuffer(
					"Class\u6570\u7EC401\u4E2D\u5B58\u5728\u5DEE\u5F02\u7C7B\uFF1A");
			for (int i = 0; i < compareList01.size(); i++)
				sb.append(compareList01.get(i).toString());

			log.debug(sb.toString());
			sb = new StringBuffer(
					"Class\u6570\u7EC402\u4E2D\u5B58\u5728\u5DEE\u5F02\u7C7B\uFF1A");
			for (int i = 0; i < compareList02.size(); i++)
				sb.append(compareList02.get(i).toString());

			log.debug(sb.toString());
			return false;
		} else {
			log.debug("Class\u6570\u7EC4\u5176\u4E2D\u4E00\u4E2A\u4E3Anull");
			return false;
		}
	}

}
